public class LinkList<E> implements List<E>{
    public LinkNode<E> head;
    public int _size;


    public void createList(E[] a){
        for(int i = 0;i<a.length;i++){
            add(a[i]);
        }
    }
    public int getSize(){
        return _size;
    }
    public void setSize(int nlen){
        if(nlen<0||nlen>_size){
            throw new IllegalArgumentException("新Size有误");
        }
        _size = nlen;
    }
    public E getElem(int i){
        if(i<0||i>=_size){
            throw new IllegalArgumentException("序号"+i+"不存在");
        }
        int index = 0;
        LinkNode<E> cur = head;
        while(index!=i){
            cur = cur.next;
            index++;
        }
        return cur.value;
    }
    public void setElem(int i,E e) {
        if(i<0||i>=_size){
            throw new IllegalArgumentException("序号"+i+"不存在");
        }
        int index = 0;
        LinkNode<E> cur = head;
        while(index!=i){
            cur = cur.next;
            index++;
        }
        cur.value = e;
    }
    public int getNo(E e) {
        int index = 0;
        LinkNode<E> cur = head;
        while(cur.next!=null){
            if(cur.value==e) {
                return index;
            }
            cur = cur.next;
            index++;
        }
        return -1;

    }
    public void add(E e){
        insert(_size,e);
    }
    public void insert(int i,E e){
        if(i<0||i>_size){
            throw new IllegalArgumentException("序号"+i+"不存在");
        }
        if(_size==0){
            LinkNode<E> newnode = new LinkNode(e);
            newnode.next = null;
            head = newnode;
        }
        else {
            int index = 0;
            LinkNode<E> newnode = new LinkNode(e);
            LinkNode<E> cur = head;
            if(i==0){
                newnode.next = head;
                head = newnode;
            }
            else {
                LinkNode<E> prev = head;
            while (index != i) {
                prev = cur;
                cur = cur.next;
                index++;
            }
            prev.next = newnode;
            newnode.next = cur;
            }
        }
        _size++;
    }
    public void delete(int i ){
        if(i<0||i>=_size){
            throw new IllegalArgumentException("序号"+i+"不存在");
        }
        int index = 0;
        LinkNode<E> cur = head;
        LinkNode<E> prev = head;
        while(index!=i){
            prev = cur;
            cur = cur.next;
            index++;
        }
        prev.next = cur.next;
    }
    public String toString(){
        String res = "";
        if(_size==0){
            return null;
        }
        else {
            LinkNode<E> cur = head;
            int k = _size;
            while (cur!=null&&k!=0) {
                res += cur.value + " ";
                cur = cur.next;
                k--;
            }
        }
        return res;
    }

    void swap(int i,int j){
        if(i<0||i>_size||j<0||j>_size){
            throw new IllegalArgumentException("序号"+i+"不存在");
        }
        int ii = i;
        int jj = j;
        LinkNode<E> n1 = head;
        LinkNode<E> n2 = head;
        while(ii!=0){
            n1 = n1.next;
            ii--;
        }
        while(jj!=0){
            n2 = n2.next;
            jj--;
        }
        E tmp = n1.value;
        n1.value = n2.value;
        n2.value = tmp;
    }
}
